﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License, as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System.Linq;
using System.Collections.Generic;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using ExtremeML.Spreadsheet;

namespace ExtremeML.Packaging
{
	// This is a partial class that adds custom functionality to its auto-generated partner.
	public sealed partial class WorkbookPartWrapper
	{
		#region Custom Properties

		/// <summary>
		/// Gets the containing package as a <see cref="SpreadsheetDocumentWrapper"/>.
		/// </summary>
		public SpreadsheetDocumentWrapper Spreadsheet
		{
			get { return (SpreadsheetDocumentWrapper)Package; }
		}

		#endregion
		#region Custom Methods

		/// <summary>
		/// Returns the <see cref="TableDefinitionPartWrapper"/> by the name of its wrapped <see cref="TableDefinitionPart"/>.
		/// </summary>
		public TableDefinitionPartWrapper GetTablePart(
			string name)
		{
			return GetAllTableParts().ToList().Find(item => item.Table.Name == name);
		}

		#endregion
		#region Overrides

		/// <summary>
		/// Implements custom persistence functionality.
		/// </summary>
		protected override void OnCustomRecomposed(
			WorkbookPart wrapped)
		{
			// Deleting the calculation chain ensures that all formula
			// cells will be re-calculated when the package is re-opened. 
			Wrapped.DeletePart(Wrapped.CalculationChainPart);
		}

		#endregion
		#region Implementation Methods

		/// <summary>
		/// Returns all table definition parts for the set of all combined worksheets in this package.
		/// </summary>
		private IEnumerable<TableDefinitionPartWrapper> GetAllTableParts()
		{
			foreach (var worksheet in WorksheetParts)
			{
				foreach (var table in worksheet.TableDefinitionParts)
				{
					yield return table;
				}
			}
		}

		#endregion
		#region Static Methods

		/// <summary>
		/// Returns a <see cref="WorkbookPart"/> populated with the default content.
		/// </summary>
		/// <param name="container">The parent <see cref="OpenXmlPartContainer"/> instance.</param>
        /// <param name="args">A parameter array of <see cref="object"/> containing member initialization arguments.</param>
        /// <returns>A new instance of <see cref="WorkbookPart"/>.</returns>
		internal static new WorkbookPart CreateCustomDefaultPart(
			OpenXmlPartContainer container,
            params object[] args)
		{
			if (container is SpreadsheetDocument)
			{
				var part = ((SpreadsheetDocument)container).AddWorkbookPart();

				WorkbookStylesPartWrapper.CreateDefaultPart(part);
				ThemePartWrapper.CreateDefaultPart(part);

				new Workbook(
					new FileVersion() { ApplicationName = "xl", LastEdited = "4", LowestEdited = "4", BuildVersion = "4505" },
					new WorkbookProperties() { DefaultThemeVersion = (UInt32Value)124226U },
					new BookViews(
						new WorkbookView() { XWindow = 360, YWindow = 135, WindowWidth = (UInt32Value)21015U, WindowHeight = (UInt32Value)9945U }),
					new Sheets(),
					new CalculationProperties() { CalculationId = (UInt32Value)124519U }
				).Save(part);

				return part;
			}
			else
			{
				return null;
			}
		}

		#endregion
	}
}
